package controller;

import java.io.IOException;
import java.util.List;

import dao.LogDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		// 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        
		LogDao dao = new LogDao();
		HttpSession session = request.getSession();
		
		//id密码是否匹配
		if(dao.logincheck(id,password)) {
			//id密码匹配后获取用户身份
			String power = dao.findpower(id);
			
			//检查对应身份，将对应身份的内容进行检查*未做
			if("student".equals(power)) {
				
				// 检查 session 中是否已经存在该用户的登录信息
			    String loggedInUser = (String) session.getAttribute("student");
			    if(loggedInUser != null && loggedInUser.equals(id)) {
			    	 // 如果已经登录，不能再次登录
			        request.setAttribute("loginError", "学生你好，请先退出登录再尝试");
			        // 转发到登录页面，显示错误消息
			        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			    }else {
	          	 // 将 Student 对象存储到 session 中
	            session.setAttribute("student", id);
	         // 验证成功，重定向
	          	response.sendRedirect("view/LoginStudent.jsp");
	          	
			    }
			}else if("fdy".equals(power)) {
				
				// 检查 session 中是否已经存在该用户的登录信息
				List<String> loggedInUser = (List<String>) session.getAttribute("fdy");
				if (loggedInUser != null && !loggedInUser.isEmpty() && loggedInUser.get(0).equals(id)) {
				 	 // 如果已经登录，不能再次登录
			        request.setAttribute("loginError", "辅导员你好，请先退出登录再尝试");
			     // 转发到登录页面，显示错误消息
			        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			    }else {
			    	List<String> idclass = dao.findclass(id) ;
			    	session.setAttribute("fdy", idclass);//这里的会话不一样是因为辅导员需要交多个班级，以后会用到
			    	response.sendRedirect("view/LoginFdy.jsp");
			    }
				
			}else if("sg".equals(power)) {
				// 检查 session 中是否已经存在该用户的登录信息
			    String loggedInUser = (String) session.getAttribute("sg");
			    if(loggedInUser != null && loggedInUser.equals(id)) {
			    	 // 如果已经登录，不能再次登录
			        request.setAttribute("loginError", "宿管你好，请先退出登录再尝试");
			        // 转发到登录页面，显示错误消息
			        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			    }else {
				session.setAttribute("sg", id);
				//再存储一个楼号好了
				session.setAttribute("sgbuild", dao.findBuild(id));
				response.sendRedirect("view/LoginSg.jsp");
			    }
			    
			}else if("wy".equals(power)) {
				// 检查 session 中是否已经存在该用户的登录信息
			    String loggedInUser = (String) session.getAttribute("wy");
			    if(loggedInUser != null && loggedInUser.equals(id)) {
			    	 // 如果已经登录，不能再次登录
			        request.setAttribute("loginError", "物业你好，请先退出登录再尝试");
			        // 转发到登录页面，显示错误消息
			        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			    }else {
				session.setAttribute("wy", id);
				response.sendRedirect("view/LoginWy.jsp");
			    }
			    
			}else if("admin".equals(power)) {
				// 检查 session 中是否已经存在该用户的登录信息
			    String loggedInUser = (String) session.getAttribute("admin");
			    if(loggedInUser != null && loggedInUser.equals(id)) {
			    	 // 如果已经登录，不能再次登录
			        request.setAttribute("loginError", "管理员你好，请先退出登录再尝试");
			        // 转发到登录页面，显示错误消息
			        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			    }else {
				session.setAttribute("admin", id);
				response.sendRedirect("view/LoginAdmin.jsp");
			    }
			    
			}else {
				 // 如果已经登录，不能再次登录
		        request.setAttribute("loginError", "账号身份未知！");
		        // 转发到登录页面，显示错误消息
		        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			}
		
		}else {
			// 如果已经登录，不能再次登录
	        request.setAttribute("loginError", "账号密码错误！");
	        // 转发到登录页面，显示错误消息
	        request.getRequestDispatcher("view/Login.jsp").forward(request, response);
			
		}
		
	}

	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        // 处理用户登出
		 	String logout = request.getParameter("logout");
	        String power = request.getParameter("power");
	        System.out.println("logout&&power:"+logout+power);
	        HttpSession session = request.getSession(false);
	        if ("true".equals(logout)) {
	            
	        	 session.removeAttribute(power);
	        }
	        response.sendRedirect("view/Login.jsp"); // 重定向到登录页面
	    }
}

